package com.sunducation.wts.databinding2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.Observable
import com.sunducation.wts.databinding2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding;
    private lateinit var user:User;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        user = User("火神", "祝融")
        binding.user = user;
        //属性第一次改变时会回调两次, 之后都只回调一次. 如果使用notifyChange()不会得到id(即i等于0). 使用
        user.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback(){
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                Log.i("MainActivity", "属性id=${propertyId}")
                if(propertyId == user.getBRFirstName()){
                    Log.i("MainActivity", "user.getBRFirstName()")
                } else if(propertyId == user.getBRLastName()) {
                    Log.i("MainActivity", "user.getBRLastName()")
                }
            }

        })
    }

    fun onClickChangeFirtName2(view: View) {
        user.firstName = "你们"
    }
    fun onClickChangeFirtName(view: View) {
        user.firstName = "我们"
    }
    fun onClickChangeLastName(view: View) {
        user.lastName = "是哦"
    }
}
